﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_BCJDLRControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        TextBox txtXkzh, txtcaseID, txtSqr, txtBsqr;
        MPCalendar calSlTimeBig, calSlTimeless;
        DropDownList ddlSzq, ddlJaqk;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack) {
                ReadSession();
                string action = base.Page.Request["action"];
                string id = base.Page.Request["id"];
                string caseid = base.Page.Request["caseid"];
                if (action == "del" && !string.IsNullOrEmpty(id))
                {
                    DelCqcjInfo(id, caseid);
                    Antu.Utilities.UtilitiesPage.AlertDialog("删除成功！", "");
                }
                DataBinderddlSzq();
                DataBinderddlJaqk();
                dataBinder();
            }
            Button btnQuery = (Button)FindControl("btnQuery");
            btnQuery.Click += new EventHandler(btnQuery_Click);
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            Button btnReset = (Button)FindControl("btnReset");
            btnReset.Click += new EventHandler(btnReset_Click);
        }

        #region 初始化控件
        protected void InitControl() {
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");

            txtXkzh = (TextBox)FindControl("txtXkzh");
            txtcaseID = (TextBox)FindControl("txtcaseID");
            txtSqr = (TextBox)FindControl("txtSqr");
            txtBsqr = (TextBox)FindControl("txtBsqr");
            calSlTimeBig = (MPCalendar)FindControl("calSlTimeBig");

            calSlTimeless = (MPCalendar)FindControl("calSlTimeless");
            ddlSzq = (DropDownList)FindControl("ddlSzq");
            ddlJaqk = (DropDownList)FindControl("ddlJaqk");
        }
        #endregion

        #region 删除案件信息
        public void DelCqcjInfo(string id, string caseid)
        {
            bool del = _EluZSDataProvider.DeleteXmByID("ZS_CASE_REGISTER", "id", id);
            if (!string.IsNullOrEmpty(caseid))
            {
                bool del1 = _EluZSDataProvider.DeleteXmByID("ZS_ONEJUSTICE", "caseid", caseid);
                bool del2 = _EluZSDataProvider.DeleteXmByID("ZS_BCJDXZFY", "caseid", caseid);
            }
        }
        #endregion

        #region 查询条件
        protected String whereValue()
        {
            String whereKey = string.Empty;
            string xkzh = txtXkzh.Text.Trim();
            string caseID = txtcaseID.Text.Trim();
            string sqr = txtSqr.Text.Trim();
            string calTimeBig = calSlTimeBig.Text;
            string calTimeLess = calSlTimeless.Text;
            string szq = ddlSzq.SelectedValue;
            string jaqk = ddlJaqk.SelectedValue;
            string bsqr = txtBsqr.Text.Trim();
            if (!string.IsNullOrEmpty(xkzh))
            {
                whereKey += " and xkzh like '" + xkzh + "%' or xkzh like '%"+ReturnString2(xkzh)+"%'";
            }
            if (!string.IsNullOrEmpty(caseID))
            {
                whereKey += " and caseid like '%" + caseID + "%' or caseid like '%" + ReturnString (caseID)+ "%'";
            }
            if (!string.IsNullOrEmpty(sqr))
            {
                whereKey += " and sqr like '%" + sqr + "%'";
            }
            if (!string.IsNullOrEmpty(calTimeBig))
            {
                whereKey += " and slsj >= to_date('" + calTimeBig + "','yyyy-MM-dd')";
            }
            if (!string.IsNullOrEmpty(calTimeLess))
            {
                whereKey += " and slsj <=to_date('" + calTimeLess + "','yyyy-MM-dd')";
            }
            if (szq != "0")
            {
                whereKey += " and szq = '" + szq + "'";
            }
            if (jaqk != "0")
            {
                whereKey += " and jaqk='" + jaqk + "'";
            }
            if (!string.IsNullOrEmpty(bsqr))
            {
                whereKey += " and bsqr like '%" + bsqr + "%'";
            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                whereKey += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||wynewpackage.GetItemTypeName('区属',t.SZQ)||'%' ";
            }
            return whereKey;

        }
        #endregion

        public string ReturnString(string txt)
        {
            string str = string.Empty;
            if (txt.Trim().Length == 8)
            {
                int i;
                if (int.TryParse(txt.Trim(), out i))
                {
                    string nian = txt.Trim().Substring(0, 4);
                    string yue = txt.Trim().Substring(4, 4);
                    str = nian + "）第" + yue;
                }
                else
                {
                    str = txt;
                }
            }
            else
            {
                str = txt;
            }
            return str;

        }

        public string ReturnString2(string txt)
        {
            string str = string.Empty;
            if (txt.Trim().Length == 7)
            {
                int i;
                if (int.TryParse(txt.Trim(), out i))
                {
                    string nian = txt.Trim().Substring(0, 4);
                    string yue = txt.Trim().Substring(4, 3);
                    str = nian + "年第" + yue;
                }
                else
                {
                    str = txt;
                }
            }
            else
            {
                str = txt;
            }
            return str;
        }

        #region 所在区
        protected void DataBinderddlSzq()
        {
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("区属");
            EluZSProvider.Params item = new Params();
            item.Keyname = "全部...";
            item.Keyvalue = "0";
            _params.Insert(0, item);
            ddlSzq.DataSource = _params;
            ddlSzq.DataValueField = "keyvalue";
            ddlSzq.DataTextField = "keyname";
            ddlSzq.DataBind();
        }
        #endregion

        #region 结案情况
        protected void DataBinderddlJaqk()
        {
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("结案情况");
            EluZSProvider.Params item = new Params();
            item.Keyname = "全部...";
            item.Keyvalue = "0";
            _params.Insert(0, item);
            ddlJaqk.DataSource = _params;
            ddlJaqk.DataValueField = "keyvalue";
            ddlJaqk.DataTextField = "keyname";
            ddlJaqk.DataBind();
        }
        #endregion

        #region 裁决案件 绑定datagieadview
        protected void dataBinder()
        {
            string whereKey = whereValue();
            DataTable dtTemp = _EluZSDataProvider.GetCaseSF(whereKey);
            dtTemp.Columns["CASEID"].ColumnName = "案件号";
            dtTemp.Columns["SLSJ"].ColumnName = "受理时间";
            dtTemp.Columns["SQR"].ColumnName = "申请人";
            dtTemp.Columns["BSQR"].ColumnName = "被申请人";
            dtTemp.Columns["FWDZ"].ColumnName = "被申请人地址";
            dtTemp.Columns["JZMJ"].ColumnName = "面积";
            dtTemp.Columns["PGJE"].ColumnName = "金额";
            dtTemp.Columns["CJSJ"].ColumnName = "裁决时间";
            dtTemp.Columns["SJAQK"].ColumnName = "结案情况";

            CustomGridView1.DataSource = dtTemp;
            CustomGridView1.RecordCount = dtTemp.Rows.Count;
            CustomGridView1.DataBind();
        }
        #endregion

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            dataBinder();
        }

        #region 查询
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SaveSession();
            dataBinder();
        }
        #endregion
        protected void btnReset_Click(object sender, EventArgs e)
        {
            base.Page.Session["xkzh"] = null;//许可证号
            base.Page.Session["caseid"] = null;//案件号
            base.Page.Session["sqr"] = null;//申请人
            base.Page.Session["bsqr"] = null;//被申请人
            base.Page.Session["SlsjBig"] = null;//受理时间大于
            base.Page.Session["Slsjless"] = null;//受理时间小于
            base.Page.Session["szq"] = null;//所在区
            base.Page.Session["jaqk"] = null;//结案情况   
            base.Page.Session["nCount"] = null;
            Antu.Utilities.UtilitiesPage.RegJsScript("window.location.href=window.location.href;");
        }
        #region 保存Session
        protected void SaveSession()
        {
            base.Page.Session["xkzh"] = txtXkzh.Text.Trim();//许可证号
            base.Page.Session["caseid"] = txtcaseID.Text.Trim();//案件号
            base.Page.Session["sqr"] = txtSqr.Text.Trim();//申请人
            base.Page.Session["bsqr"] = txtBsqr.Text.Trim();//被申请人
            base.Page.Session["SlsjBig"] = calSlTimeBig.Text;//受理时间大于
            base.Page.Session["Slsjless"] = calSlTimeless.Text;//受理时间小于
            base.Page.Session["szq"] = ddlSzq.SelectedValue;//所在区
            base.Page.Session["jaqk"] = ddlJaqk.SelectedValue;//结案情况       
        }
        #endregion

        #region 读取session
        protected void ReadSession()
        {

            txtXkzh.Text = base.Page.Session["xkzh"] == null ? "" : base.Page.Session["xkzh"].ToString();
            txtcaseID.Text = base.Page.Session["caseid"] == null ? "" : base.Page.Session["caseid"].ToString();
            txtSqr.Text = base.Page.Session["sqr"] == null ? "" : base.Page.Session["sqr"].ToString();
            txtBsqr.Text = base.Page.Session["bsqr"] == null ? "" : base.Page.Session["bsqr"].ToString();
            calSlTimeBig.Text = base.Page.Session["SlsjBig"] == null ? "" : base.Page.Session["SlsjBig"].ToString();
            calSlTimeless.Text = base.Page.Session["Slsjless"] == null ? "" : base.Page.Session["Slsjless"].ToString();
            ddlSzq.SelectedValue = base.Page.Session["szq"] == null ? "0" : base.Page.Session["szq"].ToString();
            ddlJaqk.SelectedValue = base.Page.Session["jaqk"] == null ? "0" : base.Page.Session["jaqk"].ToString();
        }
        #endregion
    }
}


